//
//  HYStatusOriginalView.swift
//  iOSWeibo
//
//  Created by why on 16/7/27.
//  Copyright © 2016年 王浩宇. All rights reserved.
//

import UIKit

class HYStatusOriginalView: UIView {
    
    ///微博模型
    var status: HYStatus? {
        didSet {
            // 当cell给原创view设置模型,根据模型来显示内容
            // 头像
            if let profile_image_url = status?.user?.profile_image_url {
                let url = NSURL(string: profile_image_url)
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
            }
            
            //名称
            nameLabel.text = status?.user?.screen_name
            
            //时间
            timeLabel.text = status?.created_at
            
            //来源
            sourceLabel.text = status?.source
            
            //会员等级 
            memberImageView.image = status?.user?.mbrankImage
            
            //认证类型
            verifiedImageView.image = status?.user?.verifiedImage
            
            //微博内容 
            contentLabel.text = status?.text
            
            // 将原创view的图片设置给配图视图
            pictureView.imageURLs = status?.storePicutureURLs
            
            // 当没有配图的时候 pictureView size = 0, 配图顶部的到contentLabel底部的间距 = 0
            pictureView.snp_updateConstraints { (make) -> Void in
                // 有图片间距为8,没有图片间距为0
                var margin: CGFloat = 0
                if status?.storePicutureURLs != nil && status?.storePicutureURLs?.count > 0 {
                    margin = CellMargin
                }
                
                make.top.equalTo(contentLabel.snp_bottom).offset(margin)
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.whiteColor()
        
        setupUI()
    }

    /// 设置UI
    private func setupUI() {
        // 1.添加自控件
        self.addSubview(iconView)
        self.addSubview(nameLabel)
        self.addSubview(timeLabel)
        self.addSubview(sourceLabel)
        self.addSubview(memberImageView)
        self.addSubview(verifiedImageView)
        self.addSubview(contentLabel)
        self.addSubview(pictureView)
        
        // 2.添加约束
        // 头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(CellMargin)
            make.size.equalTo(CGSize(width: CellIconWH, height: CellIconWH))
        }
        
        // 名称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(CellMargin)
            make.top.equalTo(iconView)
        }
        
        // 时间label
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(iconView)
        }
        
        // 来源label
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(CellMargin)
            make.centerY.equalTo(timeLabel)
        }
        
        // 会员等级
        memberImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(CellMargin)
            make.centerY.equalTo(nameLabel)
        }
        
        // 认证图标
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        
        // 微博文本内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(12)
        }
        
        // 配图视图约束
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(CellMargin)
            make.top.equalTo(contentLabel.snp_bottom).offset(CellMargin)
            
            //测试大小,其实应该根据图片的数量计算
            make.size.equalTo(CGSize(width: 359, height: 359))
        }
        
        // 约束原创view的底部和 微博文本label底部重合
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(pictureView).offset(CellMargin)
        }
    }
    
    // MARK: - 懒加载
    /// 头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar"))
    
    /// 用户名称
    private lazy var nameLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15)
    
    /// 时间label
    private lazy var timeLabel: UILabel = UILabel(textColor: UIColor.orangeColor())
    
    /// 来源label
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.lightGrayColor())
    
    /// 会员等级
    private lazy var memberImageView: UIImageView = UIImageView(frame: CGRectZero)
    
    /// 认证图标
    private lazy var verifiedImageView: UIImageView = UIImageView(frame: CGRectZero)
    
    /// 微博文本内容
    private lazy var contentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 16)
        
        // 设置多行
        label.numberOfLines = 0
        
        // 文字换行宽度
        label.preferredMaxLayoutWidth = UIScreen.width() - 2 * CellMargin
        
        return label
    }()
    
    /// 配图视图
    private lazy var pictureView: HYStatusPictureView = HYStatusPictureView()
}
